package com.moyu.shopapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moyu.shopapp.entity.Cart;
import com.moyu.shopapp.entity.Goods;
import com.moyu.shopapp.entity.User;
import com.moyu.shopapp.entity.vo.CartGoodsVO;
import com.moyu.shopapp.mapper.CartMapper;
import com.moyu.shopapp.service.CartService;
import com.moyu.shopapp.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ZJF
 * @description 针对表【cart】的数据库操作Service实现
 * @createDate 2023-12-01 16:32:18
 */

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartMapper cartMapper;

    /**
     * 通过用户id和商品id查找是否已有购物车
     * @param userId 用户id
     * @param goodsId 商品id
     * @return
     */
    @Override
    public Cart findCartByUserIdAndGoodsId(int userId, int goodsId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId,userId)
                .eq(Cart::getGoodsId,goodsId);
        return cartMapper.selectOne(wrapper);
    }

    /**
     * 获取用户购物车列表
     * @param userId 用户id
     * @return
     */
    @Override
    public List<CartGoodsVO> listByUserId(int userId) {
        List<CartGoodsVO> carts = cartMapper.selectAllByUserId(userId);
        return carts;
    }

    /**
     * 更新购物车数量
     * @param cart 购物车对象
     * @return
     */
    @Override
    public String updateQuantity(Cart cart){
        LambdaUpdateWrapper<Cart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Cart::getQuantity, cart.getQuantity())
                .eq(Cart::getCartId, cart.getCartId());
        cartMapper.update(null, updateWrapper);
        return "修改成功";
    }


}
